/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : May 11, 2012  6:38:22 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.business.process.commons.propertyunit
 * File Name : PropertyUnitProcessing.java
 * TODO
 */
package apps.business.process.commons.propertyunit;

import java.util.ArrayList;
import java.util.List;
import jpa.entitys.synchro.staging.PsWbsMaster;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import sap.business.ps.outbound.PsWbsMasterService;
import apps.business.process.SyncProcessorCommons;
import apps.promis.out.common.PropertyUnitAction;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Nov 26, 2013 : 5:14:26 PM 
 * CreateBy=Chatree Siangklom
 * 
 * PropertyUnitProcessing
 * TODO
 */
public class PropertyUnitProcessing extends SyncProcessorCommons{

	private static final Logger log = Logger.getLogger(PropertyUnitProcessing.class);
	
	public void SingleUnit(String company, String project, String buildingPhase, String zoneCode, String unitNumber) throws Exception {
		try{
			
			UPD_PROMISUseTime(true, false, null);
			PropertyUnitAction propertyUnitAction = new PropertyUnitAction();
			List<PsWbsMaster> value_list = propertyUnitAction.CreatedToStaging(company, project, buildingPhase, zoneCode, unitNumber);
			System.out.println("Created to Staging size="+ CommonService.getSize(value_list) +"---->");
			UPD_PROMISUseTime(false, true, null);
			
			if(CommonService.getSize(value_list) > 0){
				UPD_SENDUseTime(true, false, value_list.get(0).getInstanceNo());
				new PsWbsMasterService().SendingToSAP(value_list);
				UPD_SENDUseTime(false, true, value_list.get(0).getInstanceNo());
			}
			
			System.out.println("Created to SAP size="+ CommonService.getSize(value_list) +" --->");
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	public void CreatedToStagingByFloorZone(String company, String Project, String BuildingPhase, String floorZone) throws Exception 
	{
		try{
			UPD_PROMISUseTime(true, false, null);
			PropertyUnitAction propertyUnitAction = new PropertyUnitAction();
			List<PsWbsMaster> value_list = propertyUnitAction.CreatedToStagingByFloorZone(company, Project, BuildingPhase, floorZone);
			log.info("Created to Staging size="+ CommonService.getSize(value_list) +" --->");
			UPD_PROMISUseTime(false, true, null);
			
			if(CommonService.getSize(value_list) > 0)
			{
				UPD_SENDUseTime(true, false, value_list.get(0).getInstanceNo());
				new PsWbsMasterService().SendingToSAP(value_list);	
				UPD_SENDUseTime(false, true, value_list.get(0).getInstanceNo());
			}			
			log.info("Created to SAP size="+ CommonService.getSize(value_list) +"---->");
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	public void CreatedToStagingByBuilding(String company, String Project, String BuildingPhase) throws Exception 
	{
		try{
			
			UPD_PROMISUseTime(true, false, null);
			PropertyUnitAction propertyUnitAction = new PropertyUnitAction();
			List<PsWbsMaster> value_list = propertyUnitAction.CreatedToStagingByBuilding(company, Project, BuildingPhase);
			System.out.println("Created to Staging size="+ CommonService.getSize(value_list) +"--->");
			UPD_PROMISUseTime(false, true, null);
			
			
			if(CommonService.getSize(value_list) > 0)
			{
				UPD_SENDUseTime(true, false, value_list.get(0).getInstanceNo());
				new PsWbsMasterService().SendingToSAP(value_list);
				UPD_SENDUseTime(false, true, value_list.get(0).getInstanceNo());
			}
			System.out.println("Created to SAP size="+ CommonService.getSize(value_list) +" --->");
			
			
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	public void ReRunAll() {
		try{
			
			List<Object[]> lst = new PropertyUnitAction().SearchCostUnitGropProj(null, null);			
			System.out.println("Total Buidling = " + CommonService.getSize(lst));
			
			for(Object[] value : lst){
				
				System.out.println("Created and Sended to SAP ----> Plant Code = " + CommonService.IsNullRString(value[3]) + " // Unit in Building = " + CommonService.IsNullRString(value[2]));
				List<PsWbsMaster> value_list = new PropertyUnitAction().CreatedToStaging(CommonService.IsNullRString(value[2]), CommonService.IsNullRString(value[3]));
				new PsWbsMasterService().SendingToSAP(value_list);
				System.out.println("End and Sended to SAP <---- Plant Code = " + CommonService.IsNullRString(value[3]) + " // Unit in Building = " + CommonService.IsNullRString(value[2]));
				
			}
		}catch(Exception e){
			e.printStackTrace();	
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
	
	public void ProcessingAll() throws Exception{
		try{
			
			String company = "SP";
			
			List<String> lstPro = new ArrayList<String>();
			lstPro.add("CH3A1");
			lstPro.add("BY");
			lstPro.add("CH3A3");
			lstPro.add("NVOL");
			lstPro.add("SCR-R");
			lstPro.add("SCR-T");
			lstPro.add("SGV-P");
			lstPro.add("SPK-A");
			lstPro.add("SSL");
			lstPro.add("CH3A5");
			lstPro.add("CHR");
			lstPro.add("SGV5");
			lstPro.add("SP-DR");
			lstPro.add("SPKRP");
			lstPro.add("SPPC");
			lstPro.add("SRS");
			lstPro.add("SV-RB");
			lstPro.add("SVP-N");
			lstPro.add("SVTP");
			lstPro.add("VSP");
			lstPro.add("CH3A2");
			lstPro.add("CH3A6");
			lstPro.add("CH4");
			lstPro.add("CRF1");
			lstPro.add("CRF2");
			lstPro.add("CRRH");
			lstPro.add("EX");
			lstPro.add("RP");
			lstPro.add("SGVP");
			lstPro.add("SP-S");
			lstPro.add("SPK-K");
			lstPro.add("SPP-S");
			lstPro.add("SPVSR");
			lstPro.add("SVLA");
			lstPro.add("SVOS");
			lstPro.add("VP");
			lstPro.add("CGVA");
			lstPro.add("CPVA");
			lstPro.add("OP3-3");
			lstPro.add("PO");
			lstPro.add("SCH");
			lstPro.add("SCR-C");
			lstPro.add("SNVTP");
			lstPro.add("SPET");
			lstPro.add("SPK-R");
			lstPro.add("SPR");
			lstPro.add("SPVOR");
			lstPro.add("SVO-L");
			lstPro.add("SVP");
			lstPro.add("SVT");
			lstPro.add("VRAM");
			lstPro.add("CH3CT");
			lstPro.add("CRV2");
			lstPro.add("NV5");
			lstPro.add("PT");
			lstPro.add("SGVJL");
			lstPro.add("SPK-T");
			lstPro.add("SPKW");
			lstPro.add("SPV-P");
			lstPro.add("VCM");
			lstPro.add("VSK");
			lstPro.add("CH3A4");
			lstPro.add("CRV1");
			lstPro.add("SGV");
			lstPro.add("SH");
			lstPro.add("SV-PC");
			lstPro.add("SW");
			lstPro.add("VP48");
			lstPro.add("VPS");
			lstPro.add("VRK");
			lstPro.add("CHS");
			lstPro.add("SGVS");
			lstPro.add("SP-AP");
			lstPro.add("SPVR5");
			lstPro.add("SPVRS");
			lstPro.add("SV5");
			lstPro.add("SVR");
			lstPro.add("SVSP");
			lstPro.add("CH3A1");
			lstPro.add("PVD");
			lstPro.add("SGV-B");
			lstPro.add("SGV-L");
			lstPro.add("SPK-P");
			lstPro.add("SRP");
			lstPro.add("SV-SW");
			lstPro.add("SVP52");
			
			for(String project : lstPro){
				
				List<Object[]> lst = new PropertyUnitAction().SearchCostUnitGropProj(company, project);
				System.out.println("Total Buidling = " + lst.size());
		    	
				for(Object[] value : lst){
					
					//new JavaThreadUnit("", CommonService.IsNullRString(value[2]), CommonService.IsNullRString(value[3])).start();
					List<PsWbsMaster> value_list = new PropertyUnitAction().CreatedToStaging(CommonService.IsNullRString(value[2]), CommonService.IsNullRString(value[3]));
					
					//sleep(1000);
					new PsWbsMasterService().SendingToSAP(value_list);
					System.out.println("Sending to SAP size="+ CommonService.getSize(value_list) +" --->");
				}
			}
			
		}catch(Exception e){
			e.printStackTrace();			
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	private void runTest(){
		// TODO Auto-generated method stub
		try{
			
			String company = "SP";
			String project = "CHS";
			String buildingPhase = "4";
			String floorZone = "10";
			String unitNumber = "LS1004";
			
			//Create to Staging
			PropertyUnitAction propertyUnitAction = new PropertyUnitAction();
			//List<PsWbsMaster> value_list = propertyUnitAction.CreatedToStagingByFloorZone(company, project, buildingPhase, floorZone);
			
			List<PsWbsMaster> value_list = propertyUnitAction.CreatedToStaging(company, project, buildingPhase, floorZone, unitNumber);
			
			//List<PsWbsMaster> value_list = propertyUnitAction.CreatedToStaging(null, null, value.getSapPlantCode());
			System.out.println("Created to Staging size="+ CommonService.getSize(value_list) +" --->");
			
			new PsWbsMasterService().SendingToSAP(value_list);
			System.out.println("Sending to SAP size="+ CommonService.getSize(value_list) +" --->");
			
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
	
	public void ProcessingInStagingAll(String ISSYNC, String SYNC_CREATEDON) throws Exception {
		System.out.println("Start ProcessingInStagingAll [ISSYNC="+ ISSYNC +" || SYNC_CREATEDON="+ SYNC_CREATEDON +"] -->");
		
		try{
			
			List<Object[]> lst = new PropertyUnitAction().SearchUnitAllInStagingIsSync(ISSYNC, SYNC_CREATEDON);
			System.out.println("SearchUnitAllInStagingIsSync size=" + CommonService.getSize(lst));
			
			int i = 0;
			if(CommonService.getSize(lst) > 0){
				for(Object[] value : lst){
					String company = value[0].toString();
					String project = value[1].toString();
					String buildingPhase = value[2].toString();
					String floorZone = value[3].toString();
					String unitNumber = value[4].toString();
					
					List<PsWbsMaster> value_list = new PropertyUnitAction().CreatedToStaging(company, project, buildingPhase, floorZone, unitNumber);			
					System.out.println(i + ".Created to Staging size="+ CommonService.getSize(value_list) +" --->");
					
					new PsWbsMasterService().SendingToSAP(value_list);
					System.out.println(i + ".Sending to SAP size="+ CommonService.getSize(value_list) +" --->");
				}
			}
			
			/*
			List<Object[]> lst1 = new PropertyUnitAction().SearchUnitAllInStagingIsSync("'16-10-2012' ,'17-10-2012'");
			log.info("SearchUnitAllInStagingIsSync size=" + CommonService.getSize(lst1));
			i = 0;
			if(CommonService.getSize(lst1) > 0){
				for(Object[] value : lst1){
					
					String company = value[0].toString();
					String project = value[1].toString();
					String buildingPhase = value[2].toString();
					String floorZone = value[3].toString();
					String unitNumber = value[4].toString();
					
					List<PsWbsMaster> value_list = new PropertyUnitAction().CreatedToStaging(company, project, buildingPhase, floorZone, unitNumber);			
					log.info(i + ".Created to Staging size="+ CommonService.getSize(value_list) +" --->");
					System.out.println(i + ".Created to Staging size="+ CommonService.getSize(value_list) +" --->");
					
					new PsWbsMasterService().SendingToSAP(value_list);
					log.info(i + ".Sending to SAP size="+ CommonService.getSize(value_list) +" --->");
					System.out.println(i + ".Sending to SAP size="+ CommonService.getSize(value_list) +" --->");
				}
			}
			*/
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}finally{
			System.out.println("End ProcessingInStagingAll [ISSYNC="+ ISSYNC +" || SYNC_CREATEDON="+ SYNC_CREATEDON +"] -->");
		}
	}
	
	public void ProcessingNotSyncInStaging() throws Exception
	{
		try{
			List<Object[]> lst = new PropertyUnitAction().SearchNotSyncInStaging();
			System.out.println("SearchNotSyncInStaging size="+ CommonService.getSize(lst));
			int i = 0;
			if(CommonService.getSize(lst) > 0){
				for(Object [] value : lst){
					i++;
					try{
						String instanceNo = value[0].toString();
						List<PsWbsMaster> wbsLst = new PropertyUnitAction().findByInstanceNo(instanceNo);
						
						//Send to SAP
						new PsWbsMasterService().SendingToSAP(wbsLst);
						System.out.println(i + ".Sending to SAP size="+ CommonService.getSize(wbsLst) +" --->");
						
					}catch(Exception e){
						e.printStackTrace();
						log.error(e.getMessage());
						System.err.println(e.getMessage());
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	/**
	 * 
	 * Date =Nov 26, 2013 : 5:14:51 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @throws Exception
	 */
	private void runNotIsSync() throws Exception {
		try{
			
			
			List<PsWbsMaster> lst = new PropertyUnitAction().SearchAllInStaging();
			System.out.println("Created to Staging Size="+ CommonService.getSize(lst) +" --->");
			
			new PsWbsMasterService().SendingToSAP(lst);
			System.out.println("Sending to Staging Size="+ CommonService.getSize(lst) +" --->");
			
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
			
			new PropertyUnitProcessing().ProcessingInStagingAll("N", null);
			
		}catch(Exception e)
		{
			e.printStackTrace();			
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
	
}